package com.huaxin.hxfiles.service.impl;

import com.huaxin.hxfiles.service.FileUploadService;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
 * 本地存储服务接口实现类
 *
 * @author
 * @date 2025/04/04
 */
public class LocalFileUploader implements FileUploadService {

    @Value("${local.baseDirPath}")
    private String baseDirPath;

    @Override
    public void upload(String targetPath, String localFilePath) throws IOException {
        Path destPath = Path.of(baseDirPath, targetPath);
        Files.createDirectories(destPath.getParent()); // 确保目录存在
        Files.copy(Path.of(localFilePath), destPath, StandardCopyOption.REPLACE_EXISTING);
    }

    @Override
    public void upload(String targetPath, byte[] data) throws Exception {
        
    }

    @Override
    public boolean delete(String filePath) throws IOException {
        Path pathToDelete = Path.of(baseDirPath, filePath);
        return Files.deleteIfExists(pathToDelete);
    }

    @Override
    public String getDownloadUrl(String filePath) {
        // 假设你有一个Web服务器可以直接访问这个路径，返回相对应的URL
        return "file:///" + baseDirPath + "/" + filePath;
    }
}